const register =document.querySelector('.register')
const login = document.querySelector('.login')
const loginA=login.querySelector("a")
const registerA=register.querySelector("a")
const loginform=login.querySelector("form")
const registerform=register.querySelector("form")


loginA.addEventListener('click',function(e){
    login.style.display="none"
    register.style.display="block"

})
registerA.addEventListener('click',function(e){
    register.style.display="none"
    login.style.display="block"

})

registerform.addEventListener('submit',async function(e){
    e.preventDefault()
    const data=serialize(registerform,{hash:true,empty:true})
    console.log(data)
    const result= await axios({
        url:"/api/register",
        method:"post",
        data,
    })
    console.log(result)
    
    if (result.data.code==0) {
        toastr.success(result.data.message)
        // 注册成功清空表格
        registerform.reset()
        // 注册成功切回登录表单
        loginA.click()

    }
    
})
loginform.addEventListener('submit',async function(e){
    e.preventDefault()
    const data=serialize(loginform,{hash:true,empty:true})
    console.log(data)
    const result= await axios({
        url:"/api/login",
        method:"post",
        data,
    })
    console.log(result)
    
    if (result.data.code==0) {
        toastr.success(result.data.message)
        // 将token存入
        localStorage.setItem("heimatoken",result.data.token)
        // 登录成功跳转页面
        setTimeout(() => {
        location.href="index.html"
            
        }, 1000);

    }
    
})

